<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>后台登录</title>
<link rel="stylesheet" href="${BasePath}/css/bootstrap.min.css">
<link rel="stylesheet" href="${BasePath}/css/index.css?v=${cssVersion}">
<link rel="stylesheet" href="${BasePath}/css/common.css?v=${cssVersion}">
<style>
#teacherLogin {
	float: right;
	vertical-align: middle;
}
</style>
</head>
<body>
	<div class="container">
	    <div class="row">
	        <div class="col-md-offset-3 col-md-6">
	            <div class="form-horizontal">
	                <span class="heading">后台登录</span>
<!-- 	                <button class="btn btn-link" id="teacherLogin">教师登录</button> -->
	                <div class="form-group">
	                    <input type="text" class="form-control" id="username" placeholder="用户名">
	                    <i class="fa fa-user"></i>
	                </div>
	                <div class="form-group help">
	                    <input type="password" class="form-control" id="password" placeholder="密　码">
	                    <i class="fa fa-lock"></i>
	                    <a href="#" class="fa fa-question-circle"></a>
	                </div>
	                <div class="form-group">
	                    <button id="login" type="submit" class="btn btn-default">登录</button>
	                </div>
	            </div>
	        </div>
	    </div>
	</div>
	<div class="prompt"></div>

	<script type="text/javascript" src="${BasePath}/js/jquery-3.3.1.min.js"></script>  
	<script type="text/javascript" src="${BasePath}/js/bootstrap.min.js"></script>  
	<script type="text/javascript" src="${BasePath}/js/common-func.js?v=${jsVersion}"></script>  
	<script>
	$(function(){
		$.ajax({
            type: 'get',
            cache:false, 
            async:false, 
            url: "${BasePath}/signout",
            data: "",
            dataType: "json",
            success: function(data){
            	console.log(data);
            },
            error: function(request) {
            	//console.log(request);
             //prompt($(".prompt"), "系统繁忙，请稍候重试！");
            }
        });
		
		var adminstorage=window.localStorage;
		adminstorage.removeItem("ADMIN-ACCESS-TOKEN");//清除c的值
	})
	
	$("#login").click(function(){
		var username = $("#username").val();
		var password = $("#password").val();
		
		console.log('username=' + username);
		console.log('password=' + password);
		
		if(username == ''){
			prompt($(".prompt"), '请输入用户名！');
			return;
		}
		
		if(password == ''){
			prompt($(".prompt"), '请输入密码！');
			return;
		}
		
		//验证用户名和密码
		var userInfo = {
		    username: username+"&admin",
		    password: password
		};
		
		var loginResult = login(userInfo);
		console.log(loginResult);
		if(loginResult!=null){
			if(!window.localStorage){
				prompt($(".prompt"), '"浏览器需要支持localstorage"');
				return ;
			}else{
				var storage=window.localStorage;
				//写入access_token字段
				console.log(loginResult.access_token);
	            storage.setItem("ADMIN-ACCESS-TOKEN",loginResult.access_token);
	            window.location.href = "${BasePath}/page/admin/adminMain";
			}
			/* window.location.href = "${BasePath}/page/admin/teachers"; */
			
		}else{
			prompt($(".prompt"), '用户名或密码错误！');
			return;
		}
	});
	
	function login(user){
        var ret = false;
        console.log("login user:" + JSON.stringify(user));
        $.ajax({
            type: 'post',
            cache:false, 
            async:false, 
            url: "${BasePath}/page/login",
            data: user,
            dataType: "json",
            success: function(data){
            	console.log(data);
                ret = data;
            },
            error: function(request) {
             prompt($(".prompt"), "系统繁忙，请稍候重试！");
            }
        });
        return ret;
    }
	</script>
</body>
</html>